/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package it.apuliait.service.impl;

import java.util.List;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;

import it.apuliait.model.Documento;
import it.apuliait.model.LineaFattura;
import it.apuliait.model.Ricevente;
import it.apuliait.model.Trasmittente;
import it.apuliait.service.DocumentoLocalServiceUtil;
import it.apuliait.service.LineaFatturaLocalServiceUtil;
import it.apuliait.service.RiceventeLocalServiceUtil;
import it.apuliait.service.TrasmittenteLocalServiceUtil;
import it.apuliait.service.base.DocumentoLocalServiceBaseImpl;

/**
 * The implementation of the documento local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link it.apuliait.service.DocumentoLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author Donatello
 * @see it.apuliait.service.base.DocumentoLocalServiceBaseImpl
 * @see it.apuliait.service.DocumentoLocalServiceUtil
 */
public class DocumentoLocalServiceImpl extends DocumentoLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never reference this interface directly. Always use {@link it.apuliait.service.DocumentoLocalServiceUtil} to access the documento local service.
	 */
	
	 public boolean controlloDocumentoValido(long documentoId) throws PortalException, SystemException{
		   
		   boolean valid = true;
		   Documento documento = DocumentoLocalServiceUtil.getDocumento(documentoId);
		   Ricevente ricevente = RiceventeLocalServiceUtil.getRicevente(documento.getRiceventeIDFK());
		   Trasmittente trasmittente = TrasmittenteLocalServiceUtil.getTrasmittente(documento.getTrasmittenteIDFK());
		   List<LineaFattura> competenze = LineaFatturaLocalServiceUtil.getCompetenze(documento.getDocumentoId());
		
		   if(trasmittente.getCodiceFiscale().length() == 0){ valid = false; return valid; }
		   if(ricevente.getCodiceIpa().length() == 0){ valid = false; return valid; }
		   if(trasmittente.getPiva().length() == 0){ valid = false; return valid; }
		   if(documento.getTipoDocumento().equals("TD02") && trasmittente.getRagioneSociale().length() == 0){ valid = false; return valid; }
		   if(documento.getTipoDocumento().equals("TD01") && (trasmittente.getNome().length() == 0 || trasmittente.getCognome().length() == 0) ){ valid = false; return valid; }
		   if(trasmittente.getRegimeFiscale().length() == 0){ valid = false; return valid; }
		   if(trasmittente.getIndirizzoSede().length() == 0){ valid = false; return valid; }
		   if(trasmittente.getCapSede().length() == 0){ valid = false; return valid; }
		   if(trasmittente.getComuneSede().length() == 0){ valid = false; return valid; }
		   if(trasmittente.getProvinciaSede().length() == 0){ valid = false; return valid; }
		   if(trasmittente.getNazioneSede().length() == 0){ valid = false; return valid; }
		   if(ricevente.getPIvaRic().length() == 0 && ricevente.getCodFiscaleRic().length() == 0){ valid = false; return valid; }
		   if(ricevente.getIndirizzoSedeRic().length() == 0){ valid = false; return valid; }
		   if(ricevente.getCapSedeRic().length() == 0){ valid = false; return valid; }
		   if(ricevente.getComuneSedeRic().length() == 0){ valid = false; return valid; }
		   if(ricevente.getProvinciaSedeRic().length() == 0){ valid = false; return valid; }
		   if(ricevente.getNazioneSedeRic().length() == 0){ valid = false; return valid; }
		   if(documento.getTipoDocumento().length() == 0){ valid = false; return valid; }
		   if(documento.getDataDocumento().length() == 0){ valid = false; return valid; }
		   if(documento.getNumeroDocumento().length() == 0){ valid = false; return valid; }
		   for(LineaFattura competenza : competenze)
		   {
			   if(competenza.getNumeroLinea().length() == 0){ valid = false; return valid; }
			   if(competenza.getDescrizione().length() == 0){ valid = false; return valid; }
			   if(competenza.getQuantita().length() == 0){ valid = false; return valid; }
			   if(competenza.getPrezzoUnitario().length() == 0){ valid = false; return valid; }
			   if(competenza.getPrezzoTotale().length() == 0){ valid = false; return valid; }
			   if(competenza.getAliquotaIvaLinea().length() == 0){ valid = false; return valid; }
		   }
		   
		   return valid;
		   
	   }
	
}